// pages/groupbuy/groupbuy.js
var app = getApp()
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据  group_buy
   */
  data: {
    page: 1,
    page_size: 20,
    grouplistflag: true,
    showLoading: true,
    all_group_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.refreshgrouplist()
    this.setData({
      windowWidth: app.data.deviceInfo.windowWidth
    })
  },
  refreshgrouplist: function() {
    var that = this
    shihan.sendRequest(config.group_buy + this.data.page + '-' + this.data.page_size + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          response.data.data.sh_product.forEach(function(el, index) {
            el.sh_headimgurl.forEach(function(ele, index) {
              el.sh_headimgurl.push(ele)
            })
          })
          that.setData({
            page: that.data.page + 1,
            groupinfo: response.data.data,
            all_group_list: that.data.all_group_list.concat(response.data.data.sh_product),
            grouplistflag: true,
            showLoading: false
          })
          wx.stopPullDownRefresh()
          if (that.data.page == 2 && response.data.data.sh_product.length <= 20) {
            that.setData({
              grouplistflag: false,
              showLoading: false
            })
          }
        } else {
          that.setData({
            grouplistflag: false,
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  opengoodsdetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  jumpadvert: function(e) { //广告位跳转
    app.AdSkipper(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      all_group_list: [],
      showLoading: true
    });
    if (this.data.all_group_list == 0) {
      this.refreshgrouplist()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.grouplistflag) {
      that.setData({
        grouplistflag: false
      })
      this.refreshgrouplist()
    }
  }
})